/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.io.*;
import java.net.*;
import java.util.*;
/**
 *
 * @author Su Hui
 */
public class gameinfo {

    ArrayList<player> list = new ArrayList<player>();
    String ownername;
    String ownerip;
    int maxnum;

    gameinfo(String owner, String ipadd, int level,int max)
    {
        this.ownername = owner;
        this.ownerip = ipadd;
        list.add(new player(owner,ipadd,level));
        maxnum = max;

    }
    public String getgameowner()
    {
        return ownername;
    }
    public String getownerip()
    {
        return ownerip;
    }
    public String exporttostr()
    {
        String export = "active_users:";
        for (int i =0 ; i<list.size() ; i ++)
        {
            player thisone = list.get(i);
            export += thisone.username + " " +thisone.ip + " " + thisone.level;
            if(i!=list.size()-1)
                export += ",";
        }
        export += ";";

        return export;
    }
    public boolean addplayer(String user, String ipadd, int level)
    {
        player toadd = new player(user,ipadd,level);
        if(checkplayers(toadd) || maxsize() == sizeof())
            return false;
        else
        {
            
            list.add(toadd);
            return true;
        }
    }
    public boolean removeplayer(String user)
    {
        for(int i=0; i<list.size();i++)
        {
            player p = list.get(i);
            if(p.username.equals(user))
            {
                list.remove(i);
                return true;
            }
        }
        return false;
    }
    public boolean gamefull()
    {
        if(list.size() == maxnum)
            return true;
        return false;
    }
    public int sizeof()
    {
        return list.size();
    }
    public int maxsize()
    {
        return maxnum;
    }
    public String getplayerip(int i)
    {
        return list.get(i).ip;
        
    }
    private boolean checkplayers(player toadd)
    {
        for(int i = 0; i< list.size(); i++)
            if(toadd.username.equals(list.get(i).username) && toadd.ip.equals(list.get(i).ip))
                return true;
        return false;
    }


     private class player{
         String username;
         String ip;
         int level;
         player(String user, String ipadd,int level)
         {
             this.username =user;
             this.ip = ipadd;
             this.level = level;
         }
         
     }
}
